package com.szpx.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.szpx.entity.Comment;
import com.szpx.entity.User;
import com.szpx.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author lishihui
 * @date 2022/8/1 14:59
 */

@Controller
@RequestMapping(value = "/comment")
public class CommentController {
	
	
	@Autowired
	private CommentService commentService;
	
	/*
	 * TODO 此方法的用途:
	 * 查看用户详情
	 * @return
	 * @author
	 * @since 2023/3/22 0:09
	 */
	
	@GetMapping("/selectById/{id}")
	public String selectById(@PathVariable("id") Integer id, Model model) {
		Comment comment = commentService.findById(id);
		if (comment != null) {
			model.addAttribute("comment", comment);
			return "sunxinxin/comment_detail";
		} else {
			return null;
		}
	}
	
	/*
	 * TODO 此方法的用途:
	 *
	 * 删去用户
	 *
	 *
	 * @return
	 * @author
	 * @since 2023/3/22 0:09
	 */
	
	@RequestMapping("/delUser/{id}")
	public String delUser(@PathVariable("id") Integer id) {
		commentService.delComment(id);
		return "redirect:/sunxinxin/comment_index"; //重定向到用户列表请求
	}
	
	
	/*
	 * TODO 此方法的用途:
	 *
	 * 去用户修改页面
	 *
	 *
	 * @return
	 * @author
	 * @since 2023/3/22 0:09
	 */
	
	@GetMapping("/toUpdateUser/{id}")
	public String toUpdatePage(@PathVariable("id") Integer id, Model model) {
		Comment comment = commentService.findById(id);
		if (comment != null) {
			model.addAttribute("comment", comment); //将查询出来的信息 传到修改页面
			return "sunxinxin/comment_update"; //逻辑视图
		} else {
			return null;
		}
	}
	
	/*
	 * TODO 此方法的用途:
	 *
	 * 修改用户
	 *
	 *
	 * @return
	 * @author
	 * @since 2023/3/22 0:09
	 */
	
	@RequestMapping("/updateUser")
	public String updateUser(Comment comment) {
		commentService.updateComment(comment);
		return "redirect:/comment/ulist";
	}
	
	
	/*
	 * TODO 此方法的用途:
	 *
	 * 增加用户
	 *
	 *
	 * @return
	 * @author
	 * @since 2023/3/22 0:10
	 */
	
	@RequestMapping("/addUser")
	public String addUser(Comment comment) {
		commentService.addComment(comment);
		return "redirect:/comment/ulist";
	}
	
	/*
	 * TODO 此方法的用途:
	 *
	 * 去往添加用户页面
	 *
	 *
	 * @return
	 * @author
	 * @since 2023/3/23 17:28
	 */
	
	@RequestMapping("/toAddUser")
	public String toAddUser(HttpSession session) {
		return "sunxinxin/comment_add";
	}
	
	
	/*
	 * TODO 此方法的用途:
	 *
	 * 查询所有用户
	 * @return
	 * @author
	 * @since 2023/3/22 0:10
	 */
	
	@GetMapping("/ulist")
	public String getList(
			@RequestParam(defaultValue = "1") int pno,
			@RequestParam(defaultValue = "5") int pageSize,
			Comment comment, Model model) {
		PageHelper.startPage(pno, pageSize); //开始分页
		List<Comment> commentList = commentService.findByGoodsId(comment);
		PageInfo<Comment> page = new PageInfo<>(commentList);
		model.addAttribute("comments", commentList); //将查询出来的数据放入model对象中
		model.addAttribute("page", page);
		model.addAttribute("context", comment.getContext());
		model.addAttribute("goodsid", comment.getGoodsid());
		model.addAttribute("pno", pno);
		return "sunxinxin/comment_index";
	}
	
}
